package com.bwie.publish.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.publish.entity.PublishEntity;
import com.bwie.publish.service.PublishService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author wangguohai
 * @email 270901876@qq.com
 * @date 2022-04-29 09:52:30
 */
@RestController
@RequestMapping("publish/publish")
public class PublishController {
    @Autowired
    private PublishService publishService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = publishService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
    * @Author: 龍
    * @Description: 我的发布
    * @DateTime: 2022/4/29 19:18
    * @Params:
    * @Return
    */
    @RequestMapping("/mylist/{uid}")
    public R mylist(@PathVariable("uid")Integer uid){
        List<PublishEntity> pub = publishService.list(new QueryWrapper<PublishEntity>().eq("people_id", uid));
        if(pub!=null){
            return R.ok().put("data",pub);
        }
        return R.error();
    }
    
    /**
    * @Author: 龍
    * @Description: 楼市圈发布
    * @DateTime: 2022/4/29 19:38
    * @Params: 
    * @Return 
    */
    @RequestMapping("/pubAdd")
    public R pubAdd(@RequestBody PublishEntity publishEntity){
        //发布时间
        publishEntity.setPublishTime(new Date());
        //选择楼市圈
        publishEntity.setPublishType(1);
        //添加
        publishService.save(publishEntity);
        return R.ok();
    }

    /**
    * @Author: 龍
    * @Description: 二手房出售
    * @DateTime: 2022/4/29 19:42
    * @Params:
    * @Return
    */
    @RequestMapping("/resoldAdd")
    public R resoldAdd(@RequestBody PublishEntity publishEntity){
        //发布时间
        publishEntity.setPublishTime(new Date());
        //选择二手房
        publishEntity.setPublishType(2);
        //添加
        publishService.save(publishEntity);
        return R.ok();
    }
    
    /**
    * @Author: 龍
    * @Description: 出租房出售
    * @DateTime: 2022/4/29 19:44
    * @Params: 
    * @Return 
    */
    @RequestMapping("/rentalAdd")
    public R rentalAdd(@RequestBody PublishEntity publishEntity){
        //发布时间
        publishEntity.setPublishTime(new Date());
        //选择出租房
        publishEntity.setPublishType(3);
        //添加
        publishService.save(publishEntity);
        return R.ok();
    }

    /**
    * @Author: 龍
    * @Description: 我要租房
    * @DateTime: 2022/4/29 19:45
    * @Params: 
    * @Return 
    */
    @RequestMapping("/myRental")
    public R myRental(@RequestBody PublishEntity publishEntity){
        //发布时间
        publishEntity.setPublishTime(new Date());
        //用户
        publishEntity.setPeopleType(1);
        //添加
        publishService.save(publishEntity);
        return R.ok();
    }

    /**
    * @Author: 龍
    * @Description: 我要买房
    * @DateTime: 2022/4/29 19:47
    * @Params:
    * @Return
    */
    @RequestMapping("/myBuy")
    public R myBuy(@RequestBody PublishEntity publishEntity){
        //发布时间
        publishEntity.setPublishTime(new Date());
        //用户
        publishEntity.setPeopleType(1);
        //添加
        publishService.save(publishEntity);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{publishId}")
    public R info(@PathVariable("publishId") Integer publishId){
		PublishEntity publish = publishService.getById(publishId);
        return R.ok().put("publish", publish);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PublishEntity publish){
		publishService.save(publish);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PublishEntity publish){
		publishService.updateById(publish);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] publishIds){
		publishService.removeByIds(Arrays.asList(publishIds));

        return R.ok();
    }

}
